//
//  ComposeView.swift
//  weiboSwift1.0
//
//  Created by xuNick on 16/4/25.
//  Copyright © 2016年 NN. All rights reserved.
//

import UIKit

class ComposeView: UIImageView {
    
    
    //MARK: - 初始化操作
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
    //MARK: - 界面初始化 和 布局
    private func setupUI(){
        
        self.image = UIImage.getScreenShot().applyLightEffect()
        
        
        self.userInteractionEnabled = true
        
        
        
        //添加hello
        self.addSubview(helloWord)
        //布局hello
        helloWord.center = CGPointMake(self.center.x, self.center.y - 200)
        
        //添加6个button
        for value in buttons{
            self.addSubview(value)
                    //宽度,间距
                    let w = value.frame.width
                    let magin:CGFloat = (KscreenWidth - 3 * w) / 4
                    //行数.列数
                    let row = value.tag / 3
                    let col = value.tag % 3
                    //x , y
                    let x = CGFloat(col) * w + (CGFloat(col) + 1) * magin
                    let y = CGFloat(row) * w + CGFloat(row) * magin + KscreenHeight
                    
                    value.frame.origin.x = x
                    value.frame.origin.y = y
            
        }
        
        
    }
    
//    override func layoutSubviews() {
//        super.layoutSubviews()
////        
//    }
//    //MARK: - 流程控制
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for value in buttons.reverse(){
            UIView.animateWithDuration(1, delay: Double(value.tag) * 0.05 , usingSpringWithDamping: 1, initialSpringVelocity: 15, options:[], animations: { () -> Void in
                value.frame.origin.y += 300
                }, completion: nil)
        }
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, Int64(0.4 * Double(NSEC_PER_SEC))), dispatch_get_main_queue()) { () -> Void in
            
            self.removeFromSuperview()
        }

    }
    
    //MARK: - 动画控制
    override func didMoveToSuperview() {
//        
//        for value in buttons{
//            UIView.animateWithDuration(0.1, delay: Double(value.tag) * 0.5 , usingSpringWithDamping: 1, initialSpringVelocity: 15, options:[], animations: { () -> Void in
//                value.frame.origin.y -= 300
//                }, completion: nil)
//        }
        for value in buttons.reverse(){
            UIView.animateWithDuration(1, delay: Double(value.tag) * 0.05 , usingSpringWithDamping: 1, initialSpringVelocity: 15, options:[], animations: { () -> Void in
                value.frame.origin.y -= 300
                }, completion: nil)
        }

    }
    
    
    //MARK: - 懒加载控件
    //hello word
    private lazy var helloWord:UIImageView = {
        let view = UIImageView(image: UIImage(named: "compose_slogan"))
        view.sizeToFit()
        return view
    }()
    //初始化6个button
    private lazy var buttons:[ComposeMenuButton] = {
        var array = [ComposeMenuButton]()
        for i in 0..<6 {
            let button = ComposeMenuButton(textColor: UIColor.blackColor(), fontSize: 14)
            button.setImage(UIImage(named: self.buttonList[i]["icon"]!), forState: UIControlState.Normal)
             button.tag = i
            button.bounds = CGRect(x: 0, y: 0, width: (button.imageView?.frame.width)!, height: (button.imageView?.frame.height)! + 14)
            array.append(button)
            //添加监听事件
            button.addTarget(self, action: "clickButton:", forControlEvents: UIControlEvents.TouchUpInside)
            
            
            button.sizeToFit()
        }
        
        return array
        
    }()
    //给外界的闭包
    var clickAction:((dict:[String: String]) -> ())?
    var target:UIViewController?
    
    func show(target: UIViewController?) {
        target?.view.addSubview(self)
        // 记录外界传入的target
        self.target = target
    }
    
    
    
    
   @objc private func clickButton(button: UIButton){
        //告诉控制器切换
    let dict = buttonList[button.tag]
let classname = NSStringFromClass(ComposeViewVC.self)
    print(classname)
   let type = NSClassFromString(dict["className"]!) as! UIViewController.Type
    let vc = type.init()
    self.target?.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: { () -> Void in
        self.removeFromSuperview()
    })

    }
    
    
    //懒加载按钮信息数组
    private lazy var buttonList:[[String: String]] = {
       
        let path = NSBundle.mainBundle().pathForResource("compose.plist", ofType: nil)
        let array:[[String: String]] = NSArray(contentsOfFile: path!) as! [[String: String]]
        
        
        return array
    }()
}
